package com.example.duomeitijuhe.ui;


import android.app.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.duomeitijuhe.R;
import com.example.duomeitijuhe.db.OperateDatabase;
import com.example.duomeitijuhe.others.ActivityCollector;
import com.example.duomeitijuhe.ui.fragments.MainActivity;


public class LoginActivity extends Activity implements View.OnClickListener{
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button loginButton;
    private Button registerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        ActivityCollector.addActivity(this);
        initData();

        registerButton.setOnClickListener(this);

        loginButton.setOnClickListener(this);
    }
    public void initData(){
        accountEdit = (EditText)findViewById(R.id.account_edit);
        passwordEdit = (EditText)findViewById(R.id.password_edit);
        loginButton = (Button)findViewById(R.id.login_button);
        registerButton = (Button)findViewById(R.id.login_register_button);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_register_button:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_button:
                loginToDynamic();
                break;
        }
    }
    public void loginToDynamic(){
        String account = accountEdit.getText().toString();
        String password = passwordEdit.getText().toString();
        Log.i("LoginActivity","到这儿了");
        if ((!account.equals("")) && (!password.equals(""))){
            OperateDatabase operateDatabase = new OperateDatabase(LoginActivity.this,1);
            Cursor cursor = operateDatabase.queryDb("Account");
            Boolean b = false;
            if (cursor.moveToFirst()){
                do {
                    String SqlAccount = cursor.getString(cursor.getColumnIndex("account"));
                    if (account.equals(SqlAccount) ){
                        String SqlPassword = cursor.getString(cursor.getColumnIndex("password"));
                        b = true;
                        if (password.equals(SqlPassword) ){

                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            passwordEdit.setText("");
                            break;
                        }else {   ////????????????????????????????
                            Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                }while (cursor.moveToNext());

                //关闭cursor;
                cursor.close();
                operateDatabase.closeDb();

                if (b == false){
                    Toast.makeText(LoginActivity.this,"此账号没有注册过",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(LoginActivity.this,"数据库为空",Toast.LENGTH_LONG).show();
            }
        }else {
            Log.i("空值：", "账号或密码都是空的，请输入账号或密码！！！");
        }
    }

    @Override
    public void onBackPressed() {
        ActivityCollector.finishAll();

    }
}
